/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package utils;

import com.sun.codemodel.JCodeModel;
import com.sun.tools.xjc.api.ErrorListener;
import com.sun.tools.xjc.api.S2JJAXBModel;
import com.sun.tools.xjc.api.SchemaCompiler;
import com.sun.tools.xjc.api.XJC;
import java.io.File;
import org.xml.sax.InputSource;
import org.xml.sax.SAXParseException;

public class XJCGenerateJavaObj {
    public static void main(String[] args){
        try{
            String output = "src/java/";
            SchemaCompiler sc = XJC.createSchemaCompiler();
            sc.setErrorListener(new ErrorListener() {

                public void error(SAXParseException e) {
                    System.out.println("error" + e.getMessage());
                }

                public void fatalError(SAXParseException e) {
                    System.out.println("fatal" + e.getMessage());
                }

                public void warning(SAXParseException e) {
                    System.out.println("warning" + e.getMessage());
                }

                public void info(SAXParseException e) {
                    System.out.println("info" + e.getMessage());
                }
            });
            sc.forcePackageName("binding.carts");
            File schema = new File("src/java/schemas/shopcarts.xsd");
            InputSource is = new InputSource(schema.toURI().toString());
            sc.parseSchema(is);
            S2JJAXBModel model = sc.bind();
            JCodeModel code = model.generateCode(null, null);
            code.build(new File(output));
            System.out.println("Finished");
        } catch(Exception ex){
            System.out.println("Exception: " + ex.getMessage());
        }
    }
}
